/*
 *     Prionace - A file navigator to manage your files
 *
 *     Copyright (C) 2012 Francivan Bezerra
 *
 *     This file is part of Prionace.
 *
 *     Prionace is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Prionace is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Prionace.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fba.prionace.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class TabTitle extends JPanel implements ActionListener, MouseListener {

	private JLabel title;

	private JButton closeButton;

	private JTabbedPane tab;

	public TabTitle() {
		super();
		JPanel gap = new JPanel();
		gap.setOpaque(false);

		setLayout(new BorderLayout());
		setOpaque(false);
		setTitle(new JLabel());
		setCloseButton(new JButton("\u00D7"));
		getCloseButton().setToolTipText("Fechar");

		add(title, BorderLayout.WEST);
		add(gap, BorderLayout.CENTER);
		add(closeButton, BorderLayout.EAST);
	}

	public TabTitle(JLabel title) {
		this();
		setTitle(title);
	}

	public TabTitle(String title) {
		this();
		getTitle().setText(title);
		getTitle().addMouseListener(this);
	}

	public TabTitle(String title, JTabbedPane tab) {
		this(title);
		setTab(tab);
		getCloseButton().addActionListener(this);
		getCloseButton().addMouseListener(this);
	}

	public JLabel getTitle() {
		return title;
	}

	public void setTitle(JLabel title) {
		this.title = title;
	}

	public JButton getCloseButton() {
		return closeButton;
	}

	public void setCloseButton(JButton closeButton) {
		closeButton.setContentAreaFilled(false);
		closeButton.setBorderPainted(false);
		closeButton.setBorder(BorderFactory.createRaisedBevelBorder());
		this.closeButton = closeButton;
	}

	public void setTab(JTabbedPane tab) {
		this.tab = tab;
	}

	public void mouseClicked(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
		closeButton.setBorder(BorderFactory.createLoweredBevelBorder());
	}

	public void mouseReleased(MouseEvent e) {
		closeButton.setBorder(BorderFactory.createRaisedBevelBorder());

		if (e.getSource() == title) {
			int index = -1;
			JLabel tabTitle;

			do {
				index++;
				tabTitle = ((TabTitle) tab.getTabComponentAt(index)).getTitle();
			} while (e.getSource() != tabTitle);

			if (e.getButton() == MouseEvent.BUTTON1) {
				tab.setSelectedIndex(index);
			} else if (e.getButton() == MouseEvent.BUTTON2
					&& tab.getTabCount() > 1) {
				tab.remove(index);
			}
		}
	}

	public void mouseEntered(MouseEvent e) {
		if (e.getSource() == closeButton) {
			closeButton.setBorderPainted(true);
		}
	}

	public void mouseExited(MouseEvent e) {
		if (e.getSource() == closeButton) {
			closeButton.setBorderPainted(false);
		}
	}

	public void actionPerformed(ActionEvent e) {
		int index = -1;
		JButton tabButton;

		if (tab.getTabCount() > 1) {
			do {
				index++;
				tabButton = ((TabTitle) tab.getTabComponentAt(index))
						.getCloseButton();
			} while (e.getSource() != tabButton);
		}

		tab.remove(index);
	}
}
